package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.ApprovalsMapper;
import com.csr.csrcheck.pojo.Approvals;
import com.csr.csrcheck.service.ApprovalsService;
import com.csr.csrcheck.service.ex.ApprovalsException;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApprovalsServiceImpl implements ApprovalsService {
    @Resource
    private ApprovalsMapper approvalsMapper;

    public List<Approvals> getApprovalslist() {
        List<Approvals> list = this.approvalsMapper.getApprovalslist();
        if (list == null)
            throw new ApprovalsException("");
        return list;
    }

    public PageResult getApprovalslistpage(int pageNum, int pageSize, String product_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getpageinfo(pageNum, pageSize, product_name));
    }

    public void addApprovals(Approvals approvals) {
        int row = this.approvalsMapper.addApprovals(approvals);
        if (row != 1)
            throw new CompanyException("");
    }

    public int updateApprovals(Approvals approvals) {
        int row = this.approvalsMapper.updateApprovals(approvals);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Approvals findapprovalsByid(int id) {
        Approvals list = this.approvalsMapper.findapprovalsByid(id);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int deleteapprovalsByid(int id) {
        int row = this.approvalsMapper.deleteapprovalsByid(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<Approvals> getpageinfo(int pageNum, int pageSize, String product_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Approvals> list = this.approvalsMapper.getpage(product_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
